package com.jzo2o.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.market.enums.CouponStatusEnum;
import com.jzo2o.market.mapper.CouponMapper;
import com.jzo2o.market.model.domain.Activity;
import com.jzo2o.market.model.domain.Coupon;
import com.jzo2o.market.model.dto.request.CouponOperationPageQueryReqDTO;
import com.jzo2o.market.model.dto.request.MyDTO;
import com.jzo2o.market.model.dto.response.CouponInfoResDTO;
import com.jzo2o.market.service.IActivityService;
import com.jzo2o.market.service.ICouponService;
import com.jzo2o.market.service.ICouponUseBackService;
import com.jzo2o.market.service.ICouponWriteOffService;
import com.jzo2o.mysql.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-16
 */
@Service
@Slf4j
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements ICouponService {

    @Resource(name = "seizeCouponScript")
    private DefaultRedisScript<String> seizeCouponScript;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private IActivityService activityService;

    @Resource
    private ICouponUseBackService couponUseBackService;

    @Resource
    private ICouponWriteOffService couponWriteOffService;


    //单个优惠劵活动分页
    @Override
    public PageResult<CouponInfoResDTO> pageCoupon(CouponOperationPageQueryReqDTO couponOperationPageQueryReqDTO) {


        // 1. 获取分页对象
        Page<Coupon> CouponPage = PageUtils.parsePageQuery(couponOperationPageQueryReqDTO, Coupon.class);
        // 2. 执行内置分页方法
        LambdaQueryWrapper<Coupon> wrapper = Wrappers.<Coupon>lambdaQuery()
                .orderByDesc(Coupon::getCreateTime)
                .eq(couponOperationPageQueryReqDTO.getActivityId()!=null,Coupon::getActivityId,couponOperationPageQueryReqDTO.getActivityId());
        Page<Coupon> page = this.page(CouponPage, wrapper);
        // 3. 封装分页结果
        return PageUtils.toPage(page, CouponInfoResDTO.class);
    }

    @Override
    public Coupon my(MyDTO myDTO) {

        Coupon coupon = this.lambdaQuery().eq(Coupon::getStatus, myDTO.getStatus())
                .one();
        return coupon;
    }



    //用户定时任务过期优惠劵
    @Override
    public void updateUserCoupon() {
        LocalDateTime now = LocalDateTime.now();
        List<Activity> list = activityService.lambdaQuery().lt(Activity::getDistributeEndTime, now)
                .list();
        List<Long> activityIds = list.stream().map(Activity::getId).distinct().collect(Collectors.toList());

        this.lambdaUpdate().in(Coupon::getActivityId,activityIds).eq(Coupon::getStatus,CouponStatusEnum.NO_USE)
                .set(Coupon::getStatus, CouponStatusEnum.INVALID).update();
    }

}
